import sys
class Solution(object):
    def maxProfit(self, prices):
        """
        :type prices: List[int]
        :rtype: int
        """
        a = len(prices)
        if a == 0:
            return 0
        f = [[0] * 6 for _ in range(a+1)]
        f[0][1] = 0
        f[0][2] = f[0][3] = f[0][4] = f[0][5] = -sys.maxsize
        for i in range(1,a+1):
            for j in range(1,6,2):
                f[i][j] = f[i-1][j]
                if j>1 and i>1 and f[i-1][j-1]!= -sys.maxsize:
                    f[i][j] = max(f[i][j],f[i-1][j-1]+prices[i-1]-prices[i-2])
            for j in range(2,6,2):
                f[i][j] = f[i-1][j-1]
                if i>1 and f[i-1][j]!= -sys.maxsize:
                    f[i][j] = max(f[i][j],f[i-1][j]+prices[i-1]-prices[i-2])
                if j>2 and i>1 and f[i-1][j-2]!= -sys.maxsize:
                    f[i][j] = max(f[i][j], f[i - 1][j-2] + prices[i - 1] - prices[i - 2])

        return max(f[a][1],f[a][3],f[a][5])


m = [3,3,5,0,0,3,1,4]
s = Solution()
a = s.maxProfit(m)
print(a)

